<?php
/* @file
 * All signup related services.
 */

/**
 * Loads a node and parses its signup field.
 */
function _get_signups($node) {
  try {
    $element = new SimpleXMLElement($node->signup);
  }
  catch (Exception $e) {
    var_dump($node);
    var_dump($node->signup);
    var_dump($e);
  }
  return $element;
}

/**
 * Saves the signups after manipulation
 */
function _save_signups(SimpleXMLElement $element, $node) {
  try {
    $node->signup = $element->asXML();
    node_save($node);
  }
  catch (Exception $e) {
    var_dump($e);
    var_dump($node);
  }
}

function _add_user_element(SimpleXMLElement $element, $userName, SimpleXMLElement $config, $values=null) {
  $userElement = $element->addChild("user");
  $userElement->addChild("name", $userName);
  $optionsElement = $userElement->addChild("options");
  if ($values != null) {
	  foreach ($config->form->option as $option) {
	    $id = _determine_option_id($option);
	    $optionElement = $optionsElement->addChild("option", $values[$id]);
	    $optionElement->addAttribute("id", $id);
	  }
  }
}

function _determine_option_id(SimpleXMLElement $option) {
  $a = $option->attributes();
  if (((String) $a["type"]) == "boatselect") return "boatselect";
  else if ($a["id"]) return (String) $a["id"];
  throw new Exception("No option-id or unknown option-type in config.");
}

function _remove_user_signup(SimpleXMLElement $signups, $userName) {
  $doc = new DOMDocument;
  $doc->loadXML($signups->asXML());
  $signups = $doc->documentElement;
  $users = $doc->getElementsByTagName("user");
  foreach ($users as $userElement) { 
    $nameElements = $userElement->getElementsByTagName("name");
    foreach ($nameElements as $nameElement) {
      if ($nameElement->textContent == $userName) {
        $signups->removeChild($userElement);
        break;
      }
    }
  }
  $signups = new SimpleXMLElement($doc->saveXML());
  return $signups;
}

function _get_signup_element($user, $node) {
  $e = new SimpleXMLElement("<signup />");
  $e->addChild("user", _get_user_element());
  return $e;
}

function _is_user_signed_up(SimpleXMLElement $xml, $userName) {
  $xpath = "//user[name='".$userName."']";
  $result = $xml->xpath($xpath);
  if ($result === FALSE) return FALSE;
  if (sizeof($result) == 0) return FALSE;
  return TRUE;
}

function _get_boatselect_id(SimpleXMLElement $userElement) {
  $userName = (String) $userElement->name;
  $xpath = "//user[name='$userName']/options/option[@id='boatselect']";
  $result = $userElement->xpath($xpath);
  if ($result === FALSE) return FALSE;
  if (sizeof($result) != 1) {
    echo "Found no or multiple boatselect options";
    var_dump($result);
    throw new Exception("Found no or multiple boatselect options");
  }
  else {
    return (String) $result[0]; 
  }
}

function _get_experience(SimpleXMLElement $signups, $userName) {
  $xpath = "//user[name='$userName']/options/option[@id='experience']";
  $result = $signups->xpath($xpath);
  $out = "";
  if ($result === FALSE) return FALSE;
  if (sizeof($result) == 0) {
    // nothing
  } else if (sizeof($result) == 1) {
    $exp = (int) $result[0];
    $out .= "(*";
    for ($i=0; $i<$exp; $i++) {
      $out .= "*";
    }
    $out .= ")";
  } else {
    var_dump($result);
    throw new Exception("Found multiple experience elements for user $userName.");
  }
  $out .= " ";
  return $out;
}

function _remove_unfixed_signups(SimpleXMLElement $signups) {
  foreach ($signups->user as $user) {
    if (!$user['fixed'] == "true") {
      $signups = _remove_user_signup($signups, $user->name);
    }
  }
  return $signups;
}


 
